import { Controller } from "../../mvc/core/Controller";
import { GuildPastureView } from "../view/guild/pasture/GuildPastureView";
import { GuildPastureHintIcon } from "../view/guild/pasture/GuildPastureHintIcon";
import { ImagesConst } from "../resource/ImagesConst";
import { UiHintIconView } from "../view/uiIconBtn/UiHintIconView";
import { EffectManager } from "../manager/EffectManager";
import { UIIconEvent } from "../view/uiIconBtn/UIIconEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { IView } from "../../mvc/interfaces/IView";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { Game } from "../Game";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
type int = number;
//class GuildPastureController
    
    export  class GuildPastureController extends Controller
    {
       

        public get pastureView(): GuildPastureView
        {
            if (!this._pastureView) 
            {
                this._pastureView = new GuildPastureView();
            }
            return this._pastureView;
        }

        /* internal  */showHintIcon(): void
        {
            if (this._hintIcon == null) 
            {
                this._hintIcon = new GuildPastureHintIcon(ImagesConst.GuildGardenHintIcon);
            }
            if (!this._hintIcon.parent) 
            {
                this._hintIcon.show();
                UiHintIconView.instance.addHintIcon(this._hintIcon);
            }
            return;
        }

        /* internal  */hideHintIcon(): void
        {
            if (this._hintIcon != null) 
            {
                if (this._hintIcon.parent) 
                {
                    this._hintIcon.parent.removeChild(this._hintIcon);
                    EffectManager.glowFilterUnReg(this._hintIcon);
                    this._hintIcon.dispatchEvent(new UIIconEvent(UIIconEvent.RemoveIcon));
                }
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.GuildEnterPasture, this.onGuildEnterPastureHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            Dispatcher.addEventListener(EventName.GuildPastureCopyLeave, this.onGuildPastureCopyLeaveHandler);
            Dispatcher.addEventListener(EventName.GuildPastureGotoNpc, this.onGuildPastureGotoNpcHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildPastureRefreshCollectBoss, this.onGuildPastureOpenHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildPastureClearCollectBoss, this.onGuildPastureCloseHandler);
            return;
        }

        public /* override */ get view(): IView
        {
            return super.view;
        }

        /* internal  */onGuildEnterPastureHandler(arg1: DataEvent): void
        {
            GameProxy.copy.enterGuildPasture();
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1: DataEvent): void
        {
            if (!Game.sceneInfo) 
            {
                return;
            }
            if (GameMapUtil.curMapState.isGuildPasture) 
            {
                this.pastureView.show();
            }
            else 
            {
                if (this._pastureView && !this._pastureView.isHide) 
                {
                    this._pastureView.hide();
                }
                if (this._isGuildPastureOpen && cache.guild.myGuildInfo) 
                {
                    this.showHintIcon();
                }
            }
            return;
        }

        /* internal  */onGuildPastureCopyLeaveHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var onBtnClickHandler: Function;

            var loc1;
            onBtnClickHandler = null;
            e = arg1;
            onBtnClickHandler = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    if (cache.guild.myGuildInfo) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.GuildEnterManor, {"guildId": cache.guild.myGuildInfo.guildId, "useContribution": false}));
                    }
                    else 
                    {
                        GameProxy.copy.leftGroup_async();
                    }
                    Alert.resetToDefault();
                }
                return;
            }
            Alert.show(Language.getString(57591), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
            return;
        }

        /* internal  */onGuildPastureGotoNpcHandler(arg1: DataEvent): void
        {
            GameProxy.copy.enterGuildPasture();
            return;
        }

        /* internal  */onGuildPastureOpenHandler(arg1: MessageBlock): void
        {
            this._isGuildPastureOpen = true;
            if (!GameMapUtil.curMapState.isGuildPasture && cache.guild.myGuildInfo) 
            {
                this.showHintIcon();
            }
            return;
        }

        /* internal  */onGuildPastureCloseHandler(arg1: MessageBlock): void
        {
            this._isGuildPastureOpen = false;
            this.hideHintIcon();
            return;
        }

        private /* var */_pastureView: GuildPastureView;

        private /* var */_hintIcon: GuildPastureHintIcon;

        private /* var */_isGuildPastureOpen: Boolean;
    }
